extends "res://BaseEnemy.gd"

# 精英沙漠木乃伊特有属性
@export var summon_cooldown = 8.0  # 召唤冷却时间
@export var max_minions = 3  # 最大随从数量
@export var bandage_throw_cooldown = 3.0  # 绷带投掷冷却时间
@export var bandage_damage = 12  # 绷带伤害
@export var bandage_slow_factor = 0.6  # 绷带减速效果
@export var bandage_slow_duration = 2.0  # 绷带减速持续时间

var summon_timer = 0.0
var bandage_timer = 0.0
var can_summon = true
var can_throw_bandage = true
var minions = []

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置精英沙漠木乃伊特有属性
	max_health = 150
	current_health = max_health
	movement_speed = 50.0
	damage = 10
	experience_value = 30
	is_elite = true
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(1.3, 1.2, 0.8)  # 精英木乃伊颜色

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 处理召唤冷却
	if not can_summon:
		summon_timer += delta
		if summon_timer >= summon_cooldown:
			can_summon = true
			summon_timer = 0.0
	
	# 处理绷带投掷冷却
	if not can_throw_bandage:
		bandage_timer += delta
		if bandage_timer >= bandage_throw_cooldown:
			can_throw_bandage = true
			bandage_timer = 0.0
	
	# 清理已销毁的随从
	var active_minions = []
	for minion in minions:
		if is_instance_valid(minion):
			active_minions.append(minion)
	minions = active_minions
	
	# 如果可以召唤且随从数量未达上限，尝试召唤
	if can_summon and minions.size() < max_minions:
		summon_minions()
	
	# 如果可以投掷绷带且玩家存在，尝试投掷绷带
	if can_throw_bandage and target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance < 200:  # 在适当距离时投掷绷带
			throw_bandage()

func summon_minions():
	if animation_player and animation_player.has_animation("summon"):
		animation_player.play("summon")
		
		# 召唤1-2个随从
		var count = 1 + int(randf() < 0.5)
		for i in range(count):
			var minion = preload("res://DesertSkeleton.tscn").instantiate()
			get_tree().get_root().add_child(minion)
			
			# 设置随从位置（在木乃伊周围）
			var offset = Vector2(randf_range(-50, 50), randf_range(-50, 50))
			minion.global_position = global_position + offset
			
			# 设置随从属性
			minion.max_health = 20
			minion.current_health = minion.max_health
			minion.damage = 5
			minion.experience_value = 2
			
			# 添加到随从列表
			minions.append(minion)
		
		# 重置召唤冷却
		can_summon = false
		summon_timer = 0.0
		
		print("精英木乃伊召唤了", count, "个骷髅!")

func throw_bandage():
	if animation_player and animation_player.has_animation("throw"):
		animation_player.play("throw")
		
		# 创建绷带投射物
		var bandage = preload("res://BandageProjectile.tscn").instantiate()
		get_tree().get_root().add_child(bandage)
		bandage.global_position = global_position
		
		# 设置绷带属性
		var direction = (target_player.global_position - global_position).normalized()
		bandage.direction = direction
		bandage.speed = 200.0
		bandage.damage = bandage_damage
		bandage.slow_factor = bandage_slow_factor
		bandage.slow_duration = bandage_slow_duration
		bandage.source = self
		
		# 重置绷带投掷冷却
		can_throw_bandage = false
		bandage_timer = 0.0
		
		print("精英木乃伊投掷绷带!")

# 重写死亡方法，死亡时杀死所有随从
func die():
	# 杀死所有随从
	for minion in minions:
		if is_instance_valid(minion):
			minion.current_health = 0
			minion.die()
	
	# 调用父类的die方法
	super.die()
